package lib.pacificapp.datahub.model;
 
import java.util.ArrayList;
import java.util.List;

import lib.pacificapp.model.Kv;
 
import org.apache.http.protocol.HTTP;

import android.util.Log;

public class Param{
	
	 public static final int METHOD_GET=0;
	 public static final int METHOD_POST=1;
	 public static final int METHOD_TYPE_DEFAULT=2;
	 public static final int METHOD_TYPE_GET=21;
	 public static final int METHOD_TYPE_JSON=3;
    
     public  String url="";
     public void setUrl(String mUrl){
    	 url=mUrl;
     }
     public int method=METHOD_POST;
	  public   int methodType=METHOD_TYPE_DEFAULT;
	  
	  public void setMethodType(int typeId){
		  
		  methodType=typeId;
	  }
	  public  List<Kv> value;
	  public  String Encoding="UTF-8";
	  
	  public void append(String key,double val){
		  append(key,String.valueOf(val));
	  }
	  public void appends(List<Kv> _params){
		  for(int i=0;i<_params.size();i++){
			   Kv p=_params.get(i);
			    append(p.getKey(),p.getString());
		   }
	  }
	  public void append(String key,int val){
		  append(key,String.valueOf(val));
	  }
	  public void append(String key,String val){
		
		   if(value==null)
			   value=new ArrayList<Kv>();
		    try {
		   // val=URLEncoder.encode(val,Encoding);
            //  val=new String(val.getBytes(),HTTP.ISO_8859_1);//ISO_8859_1
//		        val=new String(val.getBytes(),HTTP.UTF_8);
		   } catch (Exception e) {}
		   value.add(new Kv(key,val));
		  
	  }
	   
	  public  String getValue(){
		  	if(methodType==METHOD_TYPE_JSON){
		    return getJsonString();
		  	}
		 return "";
    	 }
	  public  String getJsonString(){
			 StringBuffer jon=new StringBuffer("{");
			 boolean iss=false;
	  		 for(Kv kv:value){
	  			 if(iss){jon.append(",");
	  			 } 
	  			 iss=true;
	  			String itemVal=String.valueOf(kv.getValue());
	  			Log.d("test11",itemVal);
	  			  if(itemVal.indexOf("@json")!=-1){
	  			 	  itemVal=itemVal.replace("@json", "");
	  				   jon.append("\""+kv.getKey()+"\":"+itemVal);
	  			   }else
	  			       jon.append("\""+kv.getKey()+"\":\""+kv.getValue()+"\"");
		  			
	  			 }
	  		return jon.append("}").toString();
	  }
	  
}